import React, { Component } from 'react'
import bus from '../bus/index'
export default class Dialog extends Component {
    state={
        type:1,
        name:"",
        tel:"",
        id:-1
    }
    componentDidMount(){
        bus.on("xg",(data)=>{
            this.setState(data)
        })
    }
    input=(e)=>{
        this.setState({
            [e.target.id]:e.target.value
        })
    }
    commit=(id)=>{
        bus.emit(this.state.type===1?"msg":'xgok',{
            id:this.state.type===1?Date.now():id,
            name:this.state.name,
            tel:this.state.tel
        })
        this.setState({
            type:1,
            name:"",
            tel:""
        })
        this.props.showHide()
    }
    
  render() {
    let {name,tel,id}=this.state;
    return (
      <div style={{display:this.props.flag?'block':'none'}}>
            <input type="text" value={name} id="name" onChange={this.input} /><br />
            <input type="text" value={tel} id="tel" onChange={this.input} /><br />
            {this.state.type===1 && <button onClick={this.commit}>确认提交</button>}<br />
            {this.state.type===2 && <button onClick={this.commit.bind(this,id)}>确认修改</button>}<br />
            <button onClick={()=>this.props.showHide()}>取消</button>

      </div>
    )
  }
}
